package com.jsf.controller;

import com.jsf.pojo.Student;
import com.jsf.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class StudentController {

    @Autowired
    private StudentService studentService;

    @RequestMapping(value = "/insertStudent")
    public String insertStudent(Student student, Model model){
        int i = studentService.insertStudent(student);
        List<Student> students = studentService.selectStudent();
        model.addAttribute("stu",students);
        return "student";
    }

    @RequestMapping("/update")
    public String update(Integer id,Model model){
        model.addAttribute("id",id);
        return "/update";
    }

    @RequestMapping("/updateStudent")
    public String updateStudent(Student student, Model model){
        int j = studentService.updateStudent(student);
        List<Student> students = studentService.selectStudent();
        model.addAttribute("stu",students);
        return "student";
    }

    @RequestMapping("/deleteStudent")
    public String deleteStudent(Integer id, Model model){
        int k = studentService.deleteStudent(id);
        List<Student> students = studentService.selectStudent();
        model.addAttribute("stu",students);
        return "student";
    }

    @RequestMapping("/student")
    public String selectStudent(Model model){
        List<Student> students = studentService.selectStudent();
        model.addAttribute("stu",students);
        return "student";
    }

    @RequestMapping("/findStudentByIdOrName")
    public String findStudentByIdOrName(String select, @RequestParam(required = false)String idOrName, Model model){
        if (select.equals("id") && !idOrName.equals("")){
            Student student = null;
            try{
                student = studentService.findStudentById(Integer.valueOf(idOrName));
            }catch (Exception e){
                model.addAttribute("msg","没有任何信息！");
                System.out.println(e);
            }

            if (student == null){
                model.addAttribute("msg","没有任何信息！");
            } else {
                model.addAttribute("id",idOrName);
                model.addAttribute("studentId",student);
            }
        } else if (select.equals("name") && !idOrName.equals("")){
            List<Student> student = studentService.findStudentByName(idOrName);
            if (student.isEmpty()){
                model.addAttribute("msg","没有任何信息！");
            } else{
                model.addAttribute("name",idOrName);
                model.addAttribute("studentName",student);
            }
        } else {
            model.addAttribute("msg","没有任何信息！");
        }
        List<Student> students = studentService.selectStudent();
        model.addAttribute("stu",students);
        return "student";
    }

}
